#include<iostream>
using namespace std;
const int N=5e4+10;
int p[3*N];
int find(int x)
{
    return x==p[x]?x:p[x]=find(p[x]);
}
void unset(int x,int y)
{
    p[find(y)]=find(x);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    int ans=0;
    cin >> n >> k;
    for(int i=1;i<=3*n;i++)
    {
        p[i]=i;
    }
    while(k--)
    {
        int d,x,y;
        cin >> d >> x >> y;
        if(x>n||y>n){
            ans++;
            continue;
        }
        if(d==1){
            if(find(x)==find(y+n)||find(x)==find(y+2*n)){
                ans++;
            }else{
                unset(x,y);
                unset(x+n,y+n);
                unset(x+2*n,y+2*n);
            }
        }else if(d==2){
            if(find(x)==find(y)||find(x)==find(y+n)){
                ans++;
            }
            else{
                unset(x,y+2*n);
                unset(x+n,y);
                unset(x+2*n,y+n);
            }
        }
    }
    cout << ans;
    return 0;
}